//
//  LearningRecordTableViewCell.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/4/6.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "LearningRecordTableViewCell.h"

@implementation LearningRecordTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    [self.contentView addSubview:self.dateLabel];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.subTitleLabel];
    [self.contentView addSubview:self.statuesLabel];
    [self.contentView addSubview:self.styleImg];
    [self.contentView addSubview:self.otherImg];
    self.contentView.backgroundColor = lClearColor;
    self.backgroundColor = lClearColor;
    self.typeImgs = @[@"ic_learning_record_listen",@"ic_learning_record_listen",@"ic_learning_record_listen",@"ic_learning_record_written",@"ic_learning_record_tape",@"ic_learning_record_reallyTest",@"ic_learning_record_reallyTest"];
    return self;
}
- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.frame = CGRectMake(35,15,lScreenWidth - 35 - 30 - 50 - 10 - 36,22);
        _titleLabel.font = MFontWithSize(16);
        _titleLabel.textColor = rgba(51, 51, 51, 1);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.text = @"惠州初中英语";
    
    }
    return _titleLabel;
}
- (UILabel *)subTitleLabel
{
    if (!_subTitleLabel) {
        
        _subTitleLabel = [[UILabel alloc] init];
        _subTitleLabel.frame = CGRectMake(lScreenWidth - 10 - 50,15,50,22);
        _subTitleLabel.font = RFontWithSize(16);
        _subTitleLabel.textColor = rgba(51, 51, 51, 1);
        _subTitleLabel.textAlignment = NSTextAlignmentRight;
        _subTitleLabel.numberOfLines = 1;
        _subTitleLabel.text=@"0.00分";
    
    }
    
    return _subTitleLabel;
}
- (UILabel *)statuesLabel
{
    if (!_statuesLabel) {
        
        _statuesLabel = [[UILabel alloc] init];
        _statuesLabel.frame = CGRectMake(self.subTitleLabel.left - 10 - 20,16,20,20);
        _statuesLabel.font = MFontWithSize(12);
        _statuesLabel.textColor = lWhiteColor;
        _statuesLabel.backgroundColor = rgba(12, 183, 108, 1);
        _statuesLabel.textAlignment = NSTextAlignmentCenter;
        [_statuesLabel setAllCornersWithCornerRadius:6];
        _statuesLabel.text = @"考";
        _statuesLabel.hidden = YES;
    
    }
    return _statuesLabel;
}
- (UIImageView *)styleImg
{
    if (!_styleImg) {
        _styleImg = [[UIImageView alloc] initWithFrame:CGRectMake(10, 16, 20, 20)];
        _styleImg.image = IMAGENAMED(@"ic_learning_record_reallyTest");
    }
    return _styleImg;
}
- (UIImageView *)otherImg
{
    if (!_otherImg) {
        _otherImg = [[UIImageView alloc] initWithFrame:CGRectMake(self.titleLabel.right + 10, 19, 16, 15)];
        _otherImg.image = IMAGENAMED(@"ic_learning_record_rank");
        _otherImg.hidden = YES;
    }
    return _otherImg;
}
- (UILabel *)dateLabel
{
    if (!_dateLabel) {
        
        _dateLabel = [[UILabel alloc] init];
        _dateLabel.frame = CGRectMake(25,self.titleLabel.bottom + 10,lScreenWidth - 50,16);
        _dateLabel.font = RFontWithSize(12);
        _dateLabel.textColor = rgba(142, 142, 142, 1);
        _dateLabel.textAlignment = NSTextAlignmentLeft;
        _dateLabel.text = [self getWeekDay:1712417881];
    
    }
    return _dateLabel;
}

- (NSString*)getWeekDay:(double )time
{
  NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    
   NSDateFormatter* dateFormat = [[NSDateFormatter alloc]init];

   [dateFormat setDateFormat:@"MM/dd HH:mm"];

    NSString *str = [dateFormat stringFromDate:date];

   NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null],@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六",nil];

   NSCalendar*calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSTimeZone*timeZone = [[NSTimeZone alloc]initWithName:@"Asia/Shanghai"];

[calendar setTimeZone: timeZone];

NSCalendarUnit calendarUnit =NSCalendarUnitWeekday;

    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:date];

    return [NSString stringWithFormat:@"%@ %@",[weekdays objectAtIndex:theComponents.weekday],str];

}
- (void)setData:(NSDictionary *)data
{
    _data = data;
    
    self.subTitleLabel.text = [NSString stringWithFormat:@"%.2f分",[self.data[@"score"] doubleValue]];
    [self.subTitleLabel sizeToFit];
    self.subTitleLabel.height = 22;
    self.subTitleLabel.x = lScreenWidth - 10 - self.subTitleLabel.width;
    double maxW = lScreenWidth - 35 - 30 - self.subTitleLabel.width - 10 - 36;
    self.titleLabel.text = self.data[@"title"];
    [self.titleLabel sizeToFit];
    self.titleLabel.width = self.titleLabel.width > maxW? maxW:self.titleLabel.width;
    self.titleLabel.height = 22;
    self.otherImg.x = self.titleLabel.right + 10;
    int type = [self.data[@"question_source_type"] intValue];
    type = type >= 1? type - 1:0;
    self.styleImg.image = IMAGENAMED(self.typeImgs[type]);
    self.dateLabel.text = self.data[@"formatter_time"];
}
@end
